package cn.com.chengmandian.bike.controller;


import cn.com.chengmandian.agent.utils.AuthUtil;
import cn.com.chengmandian.bike.dto.FaultReportDto;
import cn.com.chengmandian.bike.dto.FaultReportForm;
import cn.com.chengmandian.bike.log.ActionLog;
import cn.com.chengmandian.bike.model.vo.FaultReportVo;
import cn.com.chengmandian.bike.service.IEbFaultReportService;
import cn.com.chengmandian.core.db.pojo.PageData;
import cn.com.chengmandian.core.web.pojo.AgentInfo;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 故障上报 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2021-12-16
 */
@Api(tags = "故障上报")
@RestController
@RequestMapping("/fault/report")
public class EbFaultReportController {

    @Autowired
    private IEbFaultReportService iEbFaultReportService;

    @ApiOperation("故障上报-列表分页查询")
    @GetMapping(value = "/page",name = "故障上报-列表分页查询")
    public AjaxResult<PageData<FaultReportVo>> pages(FaultReportDto faultReportDto) {
        faultReportDto.setMemberId(AuthUtil.getAgentUser().getId());
        return AjaxResult.success(PageData.build(iEbFaultReportService.getPage(faultReportDto)));
    }

    @ApiOperation("故障上报-详情查询")
    @GetMapping(value = "/detail/{id}",name = "故障上报-详情查询")
    public AjaxResult detail(@PathVariable("id")Integer id){
        return AjaxResult.success(iEbFaultReportService.detailData(id));
    }

    @ApiOperation("故障上报-新增")
    @PostMapping(value = "/add",name = "故障上报-新增")
    @ActionLog(name="故障上报-新增",description = "'参数：' + #faultReportForm")
    public AjaxResult add(@RequestBody FaultReportForm faultReportForm){
        AgentInfo agentInfo = LocalMapUtil.getAgentInfo();
        Assert.notNull(agentInfo.getAreaId(), "缺少运营区ID");
        Assert.isTrue(StrUtil.isNotBlank(faultReportForm.getBikeNo()), "缺少车辆编号");
        Assert.isTrue(StrUtil.isNotBlank(faultReportForm.getFaultType()), "缺少故障类型");
        faultReportForm.setAreaId(agentInfo.getAreaId());
        faultReportForm.setMemberId(AuthUtil.getAgentUser().getId());
        iEbFaultReportService.addData(faultReportForm);
        return AjaxResult.success();
    }

    @ApiOperation("故障上报-撤销申请")
    @PostMapping(value = "/cancel/apply/{id}",name = "故障上报-撤销申请")
    @ActionLog(name="故障上报-撤销申请",description = "'参数：' + #id")
    public AjaxResult cancelApply(@PathVariable("id")Integer id){
        iEbFaultReportService.cancelApply(id);
        return AjaxResult.success();
    }
    @ApiOperation("故障上报-删除已撤销的申请")
    @PostMapping(value = "/delete/cancel/{id}",name = "故障上报-删除已撤销申请")
    @ActionLog(name="故障上报-删除已撤销的申请",description = "'参数：' + #id")
    public AjaxResult delete(@PathVariable("id")Integer id){
        iEbFaultReportService.deleteCancelApply(id);
        return AjaxResult.success();
    }
}
